import json
import os

# 设置Json文件所在目录
json_dir = "C:/Users/EDY/Desktop/验证码_converted"

# 遍历Json文件夹中的所有json文件
for filename in os.listdir(json_dir):
    if filename.endswith(".json"):
        file_path = os.path.join(json_dir, filename)

        # 打开并读取json文件内容
        with open(file_path, "r", encoding="utf-8") as file:
            json_data = json.load(file)

        # 获取当前json的imagePath值
        image_path = json_data["imagePath"]

        # 修改label的值，并将修改后的json数据保存到文件中
        json_data["label"] = image_path
        new_file_path = os.path.join(json_dir, f"modified_{filename}")
        with open(new_file_path, "w", encoding="utf-8") as file:
            json.dump(json_data, file, ensure_ascii=False, indent=4)
